<?php

namespace app\gptcms\controller\api;
use app\gptcms\controller\BaseApi;
use think\facade\Db;
use think\facade\Session;

class Paint extends BaseApi
{
    /**
     * 获取绘画渠道
     */
    public function getPaintSet()
    {
        $wid = Session::get('wid');
        $res = Db::table('kt_gptcms_paint_msg')->where('wid',$wid)->select();
        $data = [
            [
                "title"=>"sd",
                "status"=>$res["sd"] ?? 1,
            ]，
            [
                "title"=>"mj",
                "status"=>$res["mj"] ?? 0,
            ]，
        ];
        return success('绘画模型'，$data);
    }
	/**
     * 历史记录
     */
    public function msgs()
    {
        $wid = Session::get('wid');
        $user = $this->user;
        $where = [];
        $where[] = ['wid','=',$wid];
        $where[] = ['common_id','=',$user['id']];

        $msgList = Db::table('kt_gptcms_paint_msg')->field('id,message,response')->where($where)->order('id asc')->select();
        $msgs = [];
        foreach ($msgList as $key => $msg) {
            $msgs[] = [
                'role' => '我',
                'content' => $msg['message']
            ];
            $msgs[] = [
                'role' => '助手',
                'content' => $msg['response']
            ];
        }
        return success('获取成功',$msgs);
    }

    /**
     * 清除历史记录
     */
    public function delMsgs()
    {
        $wid = Session::get('wid');
        $user = $this->user;
        if(!$user) return error('用户不存在');

        Db::table('kt_gptcms_paint_msg')->where(['wid'=>$wid,'common_id'=>$user['id']])->delete();
        return success('操作成功，已删除');
    }

    /**
     * send
     */
    public function send()
    {
    	$wid = Session::get('wid');
        $user = $this->user;
        if($user['status'] != 1){
            $this->outError('账号已停用');
        }
        $chatmodel = $this->req->param('chatmodel');
        if(!$chatmodel){
           return error('请先选择对话模型');
        }
        
        $vip = 0;
        if(strtotime($user['vip_expire']) > time()){ //会员未到期
            $vip = 1;
        }else{ //会员到期
            if($user['residue_degree'] <= 0){ //余数不足
                $zdz_remind = Db::table('kt_gptcms_system')->where('wid',$wid)->value('zdz_remind');
                return error($zdz_remind?:'剩余条数不足');
            }
        }
        
        $message = $this->req->param('message');
        if(!$message){
            return error('请输入您的描述');
        }
        // $currentTime = date('Y-m-d H:i:s', time());
        switch ($chatmodel) {
            case 'sd':
                return $this->linerAi('sd',$message);
                break;
            case 'mj':
                return $this->linerAi('mj',$message);
                break;

        }
    }

    private function linerAi($sd,$message)
    {
        $config = Db::table('kt_gptcms_gpt_config')->json(['openai','api2d','linkerai'])->where('wid',$wid)->find();
        if(!$config )  return error('未检查到配置信息');
        $aiconfig = $config['linkerai'];
        $ktadmin = new \Ktadmin\LinkerAi\Ktadmin(['channel'=>7,'api_key'=>$aiconfig['api_key']]);
        $ktadmin->chat()->sendImage($messages, $callback,['temperature'=>$aiconfig['temperature'],'max_tokens'=>$aiconfig['max_tokens'],'model'=>$aiconfig['model'],'stream'=>true]);
    }
}